package com.example.codegen;

import com.example.utils.CJUtils;

import java.util.Optional;

public class AssignStatement implements Statement {
    public DeclareExpression declareVariable;
    // please wrap the type and left expression in a bracket if you want to force cast like (int)(a.b)
    public Optional<Type> castType;
    public Expression leftExpression;

    public AssignStatement(DeclareExpression declareExpression, Optional<Type> castType, Expression expression) {
        this.declareVariable = declareExpression;
        this.castType = castType;
        this.leftExpression = expression;
    }

    public String toJava() {
        return declareVariable.toJava() + " = " + (castType.isPresent() ? "(" + castType.get().toJava() + ")" : "") + (castType.isPresent() ? "(" : "") + leftExpression.toJava() + (castType.isPresent() ? ")" : "") + ";";
    }

    public String toGo() {
        return declareVariable.toGo() + " = " + leftExpression.toGo();
    }

    @Override
    public String toJs() {
        return declareVariable.toJs() + " = " + leftExpression.toJs() + ";";
    }

    @Override
    public String toCSharp() {
        return declareVariable.toCSharp() + " = " + (castType.isPresent() ? "(" + castType.get().toCSharp() + ")" : "") + (castType.isPresent() ? "(" : "") + leftExpression.toCSharp() + (castType.isPresent() ? ")" : "") + ";";
    }

    @Override
    public String toCJ() {
        if(CJUtils.isVariableNameNormalized) {
            boolean typeChange = true;
            String left = leftExpression.toCJ();
            if (left.equals("None") || (leftExpression instanceof NewArrayExpression) || (leftExpression instanceof NewInstanceExpression) || left.contains("iNdex"))
                typeChange = false;
            if (castType.isPresent()) {
                typeChange = true;
            }
            if (typeChange)
                left = "turnClass(" + leftExpression.toCJ() + ")";
            return declareVariable.toCJ() + " = " + left;
        } else {
            if(castType.isPresent())
                return declareVariable.toCJ() + " = " +"turnClass(" + leftExpression.toCJ() + ")";
            return declareVariable.toCJ() + " = " + leftExpression.toCJ() ;
        }
    }
}
